//
//  FRQResetPwdViewController.m
//  FRQBluetoothKit_Example
//
//  Created by admin on 2023/10/21.
//  Copyright © 2023 xuchunhai. All rights reserved.
//

#import "FRQResetPwdViewController.h"
#import "ResetPwdViews.h"
#import "PasswordInputView.h"
#import "GetCodeViews.h"
#import "TabbarViewController.h"
#import "FRQViewController.h"
#import "FRQForgetDeviceViewController.h"
@interface FRQResetPwdViewController ()
@property (nonatomic,strong) ResetPwdViews *resetPwdView;
@property (nonatomic,strong) GetCodeViews *getCodeView;
@property (nonatomic,strong) PasswordInputView *passwordInputView;
@end

@implementation FRQResetPwdViewController
-(ResetPwdViews *)resetPwdView{
    if (nil==_resetPwdView) {
        _resetPwdView=skXibView(@"ResetPwdViews");
        [self.view addSubview:_resetPwdView];
        [_resetPwdView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_topLayoutGuideTop);
            make.bottom.mas_equalTo(self.mas_bottomLayoutGuideBottom);
            make.right.left.mas_equalTo(0);
        }];
        [_resetPwdView.viewPwd addSubview:self.passwordInputView];
        NSString *title=[NSString stringWithFormat:@"A verification code has been sent to\n%@",[self.dicUser objectForKey:@"userName"]];
        
        _resetPwdView.labEmail.text=title;
        
        [self.passwordInputView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.left.mas_equalTo(0);
            make.height.mas_equalTo(40);
        }];
        @weakify(self)
        [[_resetPwdView.btnBack rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            [self dismissViewControllerAnimated:YES completion:^{

            }];
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [[_resetPwdView.btnResend rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            [self sendEmail];
        }];
        [[_resetPwdView.btnKefu rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            FRQContactUsViewController *view=[[FRQContactUsViewController alloc] init];
            [self.navigationController pushViewController:view animated:YES];
        }];
    }
    return _resetPwdView;
}
-(GetCodeViews *)getCodeView{
    if (nil==_getCodeView) {
        _getCodeView=skXibView(@"GetCodeViews");
        [self.view addSubview:_getCodeView];
        [_getCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_topLayoutGuideTop);
            make.bottom.mas_equalTo(self.mas_bottomLayoutGuideBottom);
            make.right.left.mas_equalTo(0);
        }];
        @weakify(self)
        [[_getCodeView.btnBack rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [[_getCodeView.btnGetCode rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            [self resetPassword];
        }];
        [[_getCodeView.btnKefu rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            FRQContactUsViewController *view=[[FRQContactUsViewController alloc] init];
            [self.navigationController pushViewController:view animated:YES];
        }];
        NSMutableParagraphStyle *style1 = [[NSMutableParagraphStyle alloc] init];
        style1.minimumLineHeight = 0;
        NSAttributedString *attributedString1 = [[NSAttributedString alloc] initWithString:@"Email" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName : style1,NSForegroundColorAttributeName:skUIColorFromRGB(0x939598)}];
        _getCodeView.txtEmail.attributedPlaceholder = attributedString1;
        _getCodeView.txtEmail.textColor=[UIColor whiteColor];
    }
    return _getCodeView;
}
-(PasswordInputView *)passwordInputView{
    if (nil==_passwordInputView) {
        _passwordInputView=skXibView(@"PasswordInputView");
        [self.view addSubview:_passwordInputView];
    }
    return _passwordInputView;
}
//发送email验证码
- (void)sendEmail {
    NSDictionary *dic=@{
        @"accountNo": [self.dicUser objectForKey:@"userName"],
        @"type": @"1"
    };
    [skNetTools skPOSTWithURL:[NSString stringWithFormat:@"%@%@",skURL,@"app/user/sendEmail"] parameters:dic cachePolicy:0 callback:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
        NSLog(@"数据=%@",responseObject);
        skNetModel *model=responseObject;
        [SkToast SkToastShow:model.msg];
        
    }];
}
//重置密码
- (void)resetPassword {
    NSDictionary *dic=@{
        @"accountNo": [self.dicUser objectForKey:@"userName"]
    };
    
    [skNetTools skPOSTWithURL:[NSString stringWithFormat:@"%@%@",skURL,@"app/user/resetPassword"] parameters:dic cachePolicy:0 callback:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
        NSLog(@"数据=%@",responseObject);
        skNetModel *model=responseObject;
        [SkToast SkToastShow:model.msg];
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getCodeView];
    [self resetPwdView];
    if(self.viewType==0){//忘记密码
        self.resetPwdView.hidden=YES;
        self.getCodeView.hidden=NO;
        self.getCodeView.txtEmail.text=[self.dicUser objectForKey:@"userName"];
    }else if (self.viewType==1){//注册账号
        self.resetPwdView.hidden=NO;
        self.getCodeView.hidden=YES;
        [self.passwordInputView setPasswordCorrectBlock:^(NSString *pwd) {
            NSLog(@"密码是:%@",pwd);
            if(pwd.length==6){
                [self addUser:pwd];
            }
        }];
        
        // Do any additional setup after loading the view.
        
        if (!self.getCodeView.isHidden) {
            
        }else{
            
            [self sendEmail];
        }
    }else{
        
    }
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)addUser:(NSString *)code{
    //                    app/user/sendEmail
    //                    发送邮件/
    
    NSString *pwdMd5=[self.dicUser objectForKey:@"password"];
    pwdMd5=[PKNetTools md5:pwdMd5];
    pwdMd5=[PKNetTools md5:pwdMd5];
    NSDictionary *dic=@{
        @"accountNo": [self.dicUser objectForKey:@"userName"],
        @"password": pwdMd5,
        @"activationCode": [self.dicUser objectForKey:@"activationCode"],
        @"verificationCode": code
    };
    
    [skNetTools skPOSTWithURL:[NSString stringWithFormat:@"%@%@",skURL,@"app/user/add"] parameters:dic cachePolicy:0 callback:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
        NSLog(@"数据=%@",responseObject);
        skNetModel *model=[skNetModel mj_objectWithKeyValues:responseObject];
        if(model.code==200){
            NSString *token=[model.data objectForKey:@"token"];
            [skNetTools sksetToken:token];
            
            NSString *type=[model.data objectForKey:@"userType"];
            
            [skNetTools sksetUserType:type];
            
            [loginModel mj_objectWithKeyValues:model.data];
            
            NSDictionary *dic=[model.data objectForKey:@"myDevice"];
            
            if(![dic isKindOfClass:[NSNull class]]){
                [skNetTools sksetMyDevice:dic];
                CustomTabBarViewController *rootView=[[CustomTabBarViewController alloc] init];
                [[[UIApplication sharedApplication] delegate] window].rootViewController=rootView;
            }else{
                FRQForgetDeviceViewController *view=[[FRQForgetDeviceViewController alloc] init];
                view.viewType=1;
//                BaseNavoigationViewController *nav=[[BaseNavoigationViewController alloc] initWithRootViewController:view];
//                [[[UIApplication sharedApplication] delegate] window].rootViewController=nav;
                [self.navigationController pushViewController:view animated:YES];
            }
        }else{
            [SkToast SkToastShow:model.msg];
        }
    }];
}

@end
